package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApFollowBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApFollowBehaviorService;
import com.heima.model.behavior.dtos.FollowBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApFollowBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author feng js
 */
@Service
@Transactional
public class ApFollowBehaviorServiceImpl extends ServiceImpl<ApFollowBehaviorMapper,ApFollowBehavior> implements ApFollowBehaviorService {
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;


    @Override
    public ResponseResult saveFollowBehavior(FollowBehaviorDto dto) {

        ApBehaviorEntry behaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(dto.getUserId(), null);
        if (behaviorEntry == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        ApFollowBehavior followBehavior = new ApFollowBehavior();
        followBehavior.setCreatedTime(new Date());
        followBehavior.setArticleId(dto.getArticleId());
        followBehavior.setEntryId(behaviorEntry.getId());
        followBehavior.setFollowId(dto.getFollowId());
        save(followBehavior);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
